class DB {
	constructor(name, table) {
		this.name = name || 'ivcs_split_down';
		this.table = table || 'file';
		this.db = null;
	}

	/** 初始化 DB */
	openDB() {
		return new Promise((resolve, reject) => {
			const request = window.indexedDB.open(this.name, 2);
			request.onupgradeneeded = event => {
				const db = event.target.result;
				let store;
				if (!db.objectStoreNames.contains(this.table)) {
					store = db.createObjectStore(this.table, { keyPath: 'id', autoIncrement: true });
					store.createIndex('id', 'id', { unique: true });
				}
				resolve({
					statusCode: 200,
					message: '操作成功',
					data: db
				});
			};
			request.onsuccess = event => {
				const db = event.target.result;
				this.db = db;
				resolve({
					statusCode: 200,
					message: '操作成功',
					data: db
				});
			};
			request.onerror = event => {
				reject({
					statusCode: -1,
					message: '初始化失败',
					data: event
				});
			};
		});
	}
	/** 清除数据 */
	clear() {
		return new Promise(resolve => {
			const objectStore = this.db.transaction(this.table, 'readwrite').objectStore(this.table);
			objectStore.clear().onsuccess = event => {
				resolve({
					statusCode: 200,
					message: '清空成功',
					data: event
				});
			};
		});
	}
	/** 插入数据 */
	insert(data) {
		return new Promise((resolve, reject) => {
			const request = this.db.transaction([this.table], 'readwrite').objectStore(this.table).add(data);

			request.onsuccess = event => {
				resolve({
					statusCode: 200,
					message: '插入成功',
					data: event
				});
			};

			request.onerror = event => {
				reject({
					statusCode: -1,
					message: '插入失败',
					data: event
				});
			};
		});
	}
	/** 更新数据 */
	update(data) {
		return new Promise((resolve, reject) => {
			const request = this.db.transaction([this.table], 'readwrite').objectStore(this.table).put(data);
			request.onsuccess = event => {
				resolve({
					statusCode: 200,
					message: '更新成功',
					data: event
				});
			};
			request.onerror = event => {
				reject({
					statusCode: -1,
					message: '更新失败',
					data: event
				});
			};
		});
	}
	/** 删除数据 */
	del(id) {
		return new Promise((resolve, reject) => {
			const request = this.db.transaction([this.table], 'readwrite').objectStore(this.table).delete(id);
			request.onsuccess = event => {
				resolve({
					statusCode: 200,
					message: '删除成功',
					data: event
				});
			};
			request.onerror = event => {
				reject({
					statusCode: -1,
					message: '删除失败',
					data: event
				});
			};
		});
	}
	/** 根据 id 获取数据 */
	getDbDataById(id) {
		return new Promise((resolve, reject) => {
			const transaction = this.db.transaction([this.table], 'readonly');
			const store = transaction.objectStore(this.table);
			const index = store.index('id');
			const request = index.get(id);

			request.onsuccess = e => {
				const { result } = e.target;
				if (result) {
					resolve({
						statusCode: 200,
						message: '获取数据成功',
						data: result
					});
				} else {
					resolve({
						statusCode: 200,
						message: '没有找到数据',
						data: null
					});
				}
			};

			request.onerror = event => {
				reject({
					statusCode: -1,
					message: '获取数据失败',
					data: event
				});
			};
		});
	}
	/** base64 转换 */
	base64toBlob(base64, type) {
		const bstr = window.atob(base64, type);
		let n = bstr.length;
		const u8arr = new Uint8Array(n);
		while (n--) {
			u8arr[n] = bstr.charCodeAt(n);
		}
		return new Blob([u8arr]);
	}
}

export default DB;
